package org.spring.aop.support;

import com.sun.istack.internal.Nullable;
import org.aopalliance.aop.Advice;
import org.spring.aop.PointcutAdvisor;
import org.spring.core.Ordered;
import org.spring.utils.ObjectUtils;

import java.io.Serializable;

/**
 * PointcutAdvisor 实现的抽象基类。
 * 可以子类化以返回特定的切入点或可自由配置的切入点。
 *
 * @author 李坤
 * @date 2022/4/19 17:05
 */
public abstract class AbstractPointcutAdvisor implements PointcutAdvisor, Ordered, Serializable {

    /**
     * 序列值
     */
    private Integer order;

    @Override
    public int getOrder() {
        if (this.order != null) {
            return this.order;
        }

        Advice advice = this.getAdvice();
        if (advice instanceof Ordered) {
            return ((Ordered) advice).getOrder();
        }
        return Ordered.LOWEST_PRECEDENCE;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }

    @Override
    public int hashCode() {
        return PointcutAdvisor.class.hashCode();
    }

    @Override
    public boolean equals(@Nullable Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof PointcutAdvisor)) {
            return false;
        }
        PointcutAdvisor otherAdvisor = (PointcutAdvisor) other;
        return (ObjectUtils.nullSafeEquals(this.getAdvice(), otherAdvisor.getAdvice()) &&
                ObjectUtils.nullSafeEquals(this.getPointcut(), otherAdvisor.getPointcut()));
    }

}
